package com.hidtechs.popfax.view;

import java.util.HashMap;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hidtechs.popfax.R;
import com.hidtechs.popfax.controller.Constants;
import com.hidtechs.popfax.xml.ParsedExampleDataSet;

public class Login extends Base implements OnClickListener{
	/** Called when the activity is first created. */
	
	EditText userName;
	EditText password;
	Button btnSignIn;
	private ProgressDialog progressDialog ;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        userName=(EditText)findViewById(R.id.userName);
		password=(EditText)findViewById(R.id.password);
		btnSignIn=(Button)findViewById(R.id.btnSighIn);
		btnSignIn.setOnClickListener(this);
		
    }
    


	public void onClick(final View v) {
    	
    	removePreference(); // remove preferneces if any thing already there
    	
    	savePreference(userName.getText().toString(),password.getText().toString()); 
		
    	setMapValues();
    	
    	progressDialog = ProgressDialog.show(this,Constants.DIALOG_WAIT_MSG,Constants.LOGIN_DIALOG_AUTHENTICATION_MSG, true,false);
    
    	final Thread thread = new Thread(this);
        thread.start(); //invoke the run() of Base class
        
        handler = new Handler() {

    		public void handleMessage(Message msg) {
    			progressDialog.dismiss();
    	
    	
    			System.out.println("Handler");
    				 if(((ParsedExampleDataSet)(arrayOfObjectOfParsedData.get(0))).getResult()==Constants.LOGIN_SUCCESS_CODE)
    				 {
    					 Toast.makeText(getApplicationContext(),Constants.LOGGED_IN_MSG, Toast.LENGTH_SHORT).show();
    					 Intent tabIntent = new Intent(v.getContext(),com.hidtechs.popfax.view.TabWidgetController.class);
    						startActivity(tabIntent);
    				 }
    				 if(((ParsedExampleDataSet)(arrayOfObjectOfParsedData.get(0))).getResult()==Constants.LOGIN_FAILURE_CODE)
        			 {
        				 Toast.makeText(getApplicationContext(),Constants.LOGGED_IN_FAILED_MSG, Toast.LENGTH_SHORT).show();
        			 }
    			 
    		}
    		
    	};

        
	}
    
	@Override
	void setMapValues() {

		loadPreference(); 
		parameterValues = new HashMap();
		parameterValues.put(Constants.URL_ACTION,Constants.LOGIN_ACTION);
		parameterValues.put(Constants.URL_USERNAME,userName_shared_prefernece);
		parameterValues.put(Constants.URL_PASSWORD,passWord_shared_prefernece);
		
	}

	
}


